package sqlplus;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class DownloadFile {
	public static void main(String[] args) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		Blob blob = null;
		try {
			con = JdbcUtil.getConn();
			con.setAutoCommit(false);
			String sql = "select filecontent from lotus where id = ?";
			ps = con.prepareStatement(sql);
			ps.setInt(1, 15);
			rs = ps.executeQuery();
			if (rs.next()) {
				blob = rs.getBlob(1);
			}
			InputStream in = blob.getBinaryStream();
			OutputStream out = new FileOutputStream("../../downloadfile.jpg");
			int b = 0;
			int c = 0;
			while ((b = in.read()) != -1) {
				out.write(b);
				if (++c % 1024 == 0) {
					System.out.println("We Download file to :" + c / 1024
							+ "kb......");

				}
			}
			System.out.println("All read :" + c / 1024 + "kb");
			in.close();
			out.close();
			con.commit();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				con.rollback();
			} catch (Exception ee) {
				ee.printStackTrace();
			}
		} finally {
		}
		JdbcUtil.close(rs, ps, con);
	}

}
